/* 
 * File:   makelp.cpp
 * Author: sergey
 *
 * Created on 9 Декабрь 2009 г., 23:25
 */
#include "DefineConfiguration.h"
#include "BooleanFunction.h"
#include "FullLibrary.h"
#include "LPLibrary.h"
#include "LibraryInterface.h"
#include <iostream>
#include <sstream>
#include <string>

const UInt32 n = 3;
typedef LibraryInfo<n> CurLibInfo;

int main() {
    FullLibrary<CurLibInfo::pre> libraryPre;
    std::ostringstream filename;
    filename << LIBRARYDIRECTORIES << "/fulllibs/fulllibrary_" << CurLibInfo::pre::n << ".bin";
    libraryPre.load(filename.str());
    filename.str("");

    LPLibrary<CurLibInfo> library;
    
    UInt8 ts, ts1, ts2, ts3;
    UInt64 f1, f2, f1p, f2p;
    
    for(UInt64 f = 0, i = 1; f < CurLibInfo::fC; f++)
        if (!library.isSet(f)) {            
            i++;
            ts = -1;
            f1p = (f >> (CurLibInfo::aC >> 1));
            f2p = (f & ~AttributeMask<CurLibInfo, 0>::value);
            for(UInt64 f3 = 0; f3 < CurLibInfo::pre::fC; f3++) {
                f1 = f3 ^ f1p;
                f2 = f3 ^ f2p;
                libraryPre.get(f1, ts1);
                libraryPre.get(f2, ts2);
                libraryPre.get(f3, ts3);

                if (ts1 + ts2 + ts3 < ts)
                    ts = ts1 + ts2 + ts3;
            }
            library.set(f, ts);
            std::cout << std::dec << i << ' ' << std::hex << f << std::dec << ' ' << (int)ts << std::endl;
        }

    filename << LIBRARYDIRECTORIES << "/lpblibs/lplibrary_" << CurLibInfo::n << ".bin";
    library.save(filename.str());

    return (0);
}

